package net.sf.odinms.scripting.rhinoEngine;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import java.util.Arrays;
import java.util.List;

public class RhinoScriptEngineFactory implements ScriptEngineFactory {

    @Override
    public String getEngineName() {
        return "Rhino Script Engine Wrapper";
    }

    @Override
    public String getEngineVersion() {
        return "1.0";
    }

    @Override
    public List<String> getExtensions() {
        return Arrays.asList("js");
    }

    @Override
    public List<String> getMimeTypes() {
        return Arrays.asList("application/javascript", "text/javascript");
    }

    @Override
    public List<String> getNames() {
        return Arrays.asList("rhino-wrapper", "javascript-wrapper");
    }

    @Override
    public String getLanguageName() {
        return "javascript";
    }

    @Override
    public String getLanguageVersion() {
        return "1.8";
    }

    @Override
    public Object getParameter(String key) {
        switch (key) {
            case ScriptEngine.ENGINE: return getEngineName();
            case ScriptEngine.ENGINE_VERSION: return getEngineVersion();
            case ScriptEngine.NAME: return getNames().get(0);
            case ScriptEngine.LANGUAGE: return getLanguageName();
            case ScriptEngine.LANGUAGE_VERSION: return getLanguageVersion();
            default: return null;
        }
    }

    @Override
    public String getMethodCallSyntax(String obj, String method, String... args) {
        // 简单实现
        return obj + "." + method + "(" + String.join(", ", args) + ")";
    }

    @Override
    public String getOutputStatement(String toDisplay) {
        return "print('" + toDisplay + "')";
    }

    @Override
    public String getProgram(String... statements) {
        return String.join(";\n", statements);
    }

    @Override
    public ScriptEngine getScriptEngine() {
        return new RhinoScriptEngineWrapper(this);
    }
}
